<?php
namespace App\Services;
use GuzzleHttp\Client;
use App\Models\Officialusersinfo;
use Illuminate\Support\Facades\Storage;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Redis;
use Log,Exception,Config;
use Symfony\Component\Cache\Adapter\RedisAdapter;

class MiniappsetService
{
    public static function getMiniappInfo($appid,$data)
    {
        $params = self::requestData($appid,$data);
        $client = new Client([
            'verify' =>false
        ]);
        $response = $client->request('POST', 'https://third.wailian1.cn/api/version/1/miniapp/index_info', [
            'form_params' => $params
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        $results = self::apiAesDecrypt($params["key"],$jsonbody["iv"],$jsonbody["data"]);
        return $results;
    }

    public static function getMiniappActivityDetial($appid,$data)
    {
        $params = self::requestData($appid,$data);
        $client = new Client;
        $response = $client->request('POST', 'https://third.wailian1.cn/api/version/1/miniapp/activity_detial', [
            'form_params' => $params
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        $results = self::apiAesDecrypt($params["key"],$jsonbody["iv"],$jsonbody["data"]);
        return $results;
    }

    public static function deleteMiniappActivity($appid,$data)
    {
        $params = self::requestData($appid,$data);
        $client = new Client;
        $response = $client->request('POST', 'https://third.wailian1.cn/api/version/1/miniapp/activity_delete', [
            'form_params' => $params
        ]);
        $body = $response->getBody();
        $results = json_decode($body,true);
        return $results;
    }

    public static function updateMiniappActivity($appid,$data)
    {
        $params = self::requestData($appid,$data);
        $client = new Client;
        $response = $client->request('POST', 'https://third.wailian1.cn/api/version/1/miniapp/activity_update', [
            'form_params' => $params
        ]);
        $body = $response->getBody();
        $results = json_decode($body,true);
        return $results;
    }

    public static function updateMiniappIndexSetting($appid,$data)
    {
        $params = self::requestData($appid,$data);
        $client = new Client;
        $response = $client->request('POST', 'https://third.wailian1.cn/api/version/1/miniapp/index_setting', [
            'form_params' => $params
        ]);
        $body = $response->getBody();
        $results = json_decode($body,true);
        return $results;
    }

    public static function addMiniappActivity($appid,$data)
    {
        $params = self::requestData($appid,$data);
        $client = new Client;
        $response = $client->request('POST', 'https://third.wailian1.cn/api/version/1/miniapp/activity_add', [
            'form_params' => $params
        ]);
        $body = $response->getBody();
        $results = json_decode($body,true);
        return $results;
    }

    public static function miniappAdd($appid,$data)
    {
        $params = self::requestData($appid,$data);
        $client = new Client;
        $response = $client->request('POST', 'https://third.wailian1.cn/api/version/1/miniapp/miniapp_add', [
            'form_params' => $params
        ]);
        $body = $response->getBody();
        $results = json_decode($body,true);
        return $results;
    }

    public static function requestData($appid,$data)
    {
        $iv = time();
        $key = substr($appid,0,16);
        $sign = md5("Wlm".$iv.$key.$iv."mlW");
        $data = self::apiAesEncrypt($key,$iv,$data);
        return ["key"=>$key,"iv"=>$iv,"sign"=>$sign,"data"=>$data];
    }

    public static function apiAesEncrypt($key,$iv,$data)
    {
        $iv = 'ttwlv1'.$iv;
        $encrypted = base64_encode(openssl_encrypt(json_encode($data),"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv));
        return $encrypted;
    }

    public static function apiAesDecrypt($key,$iv,$data)
    {
        $iv = 'ttwlv1'.$iv;
        $decrypted = openssl_decrypt(base64_decode($data), 'AES-128-CBC', $key,OPENSSL_RAW_DATA,$iv);
        return json_decode($decrypted);
    }
}
